package com.bbs.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.bbs.comm.SysConstant;
import com.bbs.dao.UserDao;
import com.bbs.dao.impl.SectionDaoImpl;
import com.bbs.dao.impl.UserDaoImpl;
import com.bbs.model.Section;
import com.bbs.model.User;
import com.bbs.util.EncrypterUti;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class UserAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserDao userDao;
	private User user;
	private List<User> userList;
	private List<Section> sectionList;
	private Section section;
	
	//添加用户
	public String addUser() throws IOException{
		userDao=new UserDaoImpl();
		user.setUser_register_time
			(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		if(user.getUser_password().equals(user.getRe_password())){
			user.setUser_password(EncrypterUti.encrypt(user.getUser_password()));
		}
		userDao.save(user);
		return "addUser"; 
	}
	//删除用户
	public String delUser() throws IOException{
		userDao=new UserDaoImpl();
		userDao.delete(user.getUser_id());
		return "delUser"; 
	}
	
	//用户注册
	public String userRegister() throws IOException{
		userDao=new UserDaoImpl();
		//设置用户注册时间
		user.setUser_register_time
		(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		//判断用户密码与确认密码是否相等
		if(user.getUser_password().equals(user.getRe_password())){
			//设置加密的密码
			user.setUser_password(EncrypterUti.encrypt(user.getUser_password()));
		}
		//存入数据库
		userDao.save(user);
		return "userRegister"; 
	}
	//显示所有用户
	public String listUser() throws IOException{
		userDao=new UserDaoImpl();
		userList=userDao.listUser();
		return "listUser";
	}
	//修改用户
	public String updateUser() throws IOException{
		userDao=new UserDaoImpl();
		userDao.update(user);
		return "updateUser";
	}
	//用户修改密码
	public String changePassword() throws IOException{
		// To do 
		return "changePassword";
	}
	//管理员重置密码
	public String resetPassword() throws IOException{
		userDao=new UserDaoImpl();
		user.setUser_password(EncrypterUti.encrypt(user.getUser_name()));
		userDao.resetPassword(user);
		return "listUser";
	}
	
	//设为版主
	public String setModerator() throws IOException{
		userDao=new UserDaoImpl();
		//用role_id 来存从前台传来的部门id
		user.setUser_role_id(SysConstant.Moderator+"_"+user.getUser_role_id());
		//setRole 比setModerator 通用些
		userDao.setRole(user);
		return "setModerator";
	}
	//撤销版主
	public String dismissModerator() throws IOException{
		//要删除的版块
		String sdToDel=section.getSection_id()+"";
		
		userDao=new UserDaoImpl();
		user=userDao.findById(user.getUser_id());
		String oldRoleId=user.getUser_role_id();
		String newRoleId = null;
		String[] scId=oldRoleId.split("_");
		for(int i=1;i<scId.length;i++){
			if (!scId[i].equals(sdToDel)) {
				newRoleId+="_"+scId[i]+"_";
			}
		}
		if(newRoleId!=null){
			user.setUser_role_id(SysConstant.Moderator+newRoleId);
		}else {
			user.setUser_role_id(SysConstant.User);
		}
		//setRole 比setModerator 通用些
		userDao.setRole(user);
		return "dismissModerator";
	}
	//显示设为版主页面
	public String showSetModerator() throws IOException{
		SectionDaoImpl sectionDao = new SectionDaoImpl();
		setSectionList(sectionDao.findAll());
		return "showSetModerator";
	}
	//显示撤销版主页面
	public String showDismissModerator() throws IOException{
		userDao=new UserDaoImpl();
		SectionDaoImpl sectionDao = new SectionDaoImpl();
		user=userDao.findById(user.getUser_id());
		String roleid=user.getUser_role_id();
		String[] sid=roleid.split("_");
		for(String sectionid:sid){
			Section section;
			section=sectionDao.findById(Integer.parseInt(sectionid));
			sectionList.add(section);
		}
		return "showDismissModerator";
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public List<Section> getSectionList() {
		return sectionList;
	}
	public void setSectionList(List<Section> sectionList) {
		this.sectionList = sectionList;
	}
	public Section getSection() {
		return section;
	}
	public void setSection(Section section) {
		this.section = section;
	}
}
